/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Constant;
import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Course;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Review;
import com.hboxs.asl.service.CourseService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller - 评论
 */
@Controller("adminReviewController")
@RequestMapping("/asl-admin/review")
public class ReviewController extends BaseController {

    @Resource(name = "reviewServiceImpl")
    private ReviewService reviewService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;


    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        List<Member> members = memberService.findAll();
        List<Course> courses = courseService.findAll();
        model.addAttribute("members", members);
        model.addAttribute("courses", courses);
        model.addAttribute("score", Constant.score);
        return "/admin/review/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Review review, Long memberId, Long courseId, RedirectAttributes redirectAttributes) {
        try {
            if (review == null) {
                return ERROR_VIEW;
            }
            Member member = memberService.find(memberId);
            review.setMember(member);
            Course course = courseService.find(courseId);
            review.setCourse(course);
            Member teacher = course.getMember();
            review.setTeacher(teacher);
            reviewService.save(review);
        } catch (Exception e) {
            e.printStackTrace();
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("review", reviewService.find(id));
        model.addAttribute("score", Constant.score);
        return "/admin/review/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(String content,Review review, Long courseId, Long memberId, RedirectAttributes redirectAttributes) {
        Member member = memberService.find(memberId);
        if (member != null){
            review.setMember(member);
        }
        Course course = courseService.find(courseId);
        if (course != null) {
            review.setCourse(course);
        }
        review.setContent(content);
        reviewService.update(review);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Review.Type type, Pageable pageable, ModelMap model) {
        model.addAttribute("type", type);
        model.addAttribute("types", Review.Type.values());
        model.addAttribute("page", reviewService.findPage(null, null, type, null, pageable));
        return "/admin/review/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        reviewService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}